//
//  PropertyGenderCell.m
//  Trackit
//
//  Created by yaodaobin on 2017/6/30.
//  Copyright © 2017年 yaodaobin. All rights reserved.
//

#import "PropertyGenderCell.h"
#import "RichButton.h"

@interface PropertyGenderCell()

@property (nonatomic, strong) UIView *genderView;
@property (nonatomic, strong) RichButton *femaleRichBtn;
@property (nonatomic, strong) RichButton *maleRichBtn;

@end

@implementation PropertyGenderCell

- (void)awakeFromNib {
    [super awakeFromNib];
    [self initUI];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initUI];
    }
    return self;
}

- (void)initUI {
    kWeakSelf
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.font = [UIFont systemFontOfSize:14];
    self.titleLabel.textColor = [UIColor grayColor];
    [self.contentView addSubview:self.titleLabel];
    
    self.genderView = [[UIView alloc] init];
    [self.contentView addSubview:self.genderView];
    
    self.femaleRichBtn = [[RichButton alloc] init];
    self.femaleRichBtn.style = RichButtonImgLeft;
    [self.femaleRichBtn setImage:[UIImage imageNamed:@"icon_unselected_state"] forState:UIControlStateNormal];
    [self.femaleRichBtn setImage:[UIImage imageNamed:@"icon_selected_state"] forState:UIControlStateSelected];
    [self.femaleRichBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.femaleRichBtn setTitle:@"女" forState:UIControlStateNormal];
    self.femaleRichBtn.richFont = self.titleLabel.font;
    self.femaleRichBtn.interval = @5;
    self.femaleRichBtn.selected = YES;
    [self.genderView addSubview:self.femaleRichBtn];
    self.femaleRichBtn.tappedBlock = ^(UIButton *btn) {
        weakSelf.femaleRichBtn.selected = !weakSelf.femaleRichBtn.selected;
        weakSelf.maleRichBtn.selected = !weakSelf.femaleRichBtn.selected;
        weakSelf.item.inputTitle = weakSelf.femaleRichBtn.titleLabel.text;
    };

    self.maleRichBtn = [[RichButton alloc] init];
    self.maleRichBtn.style = RichButtonImgLeft;
    [self.maleRichBtn setImage:[UIImage imageNamed:@"icon_unselected_state"] forState:UIControlStateNormal];
    [self.maleRichBtn setImage:[UIImage imageNamed:@"icon_selected_state"] forState:UIControlStateSelected];
    [self.maleRichBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.maleRichBtn setTitle:@"男" forState:UIControlStateNormal];
    self.maleRichBtn.richFont = self.titleLabel.font;
    self.maleRichBtn.interval = @5;
    [self.genderView addSubview:self.maleRichBtn];
    self.maleRichBtn.tappedBlock = ^(UIButton *btn) {
        weakSelf.maleRichBtn.selected = !weakSelf.maleRichBtn.selected;
        weakSelf.femaleRichBtn.selected = !weakSelf.maleRichBtn.selected;
        weakSelf.item.inputTitle = weakSelf.maleRichBtn.titleLabel.text;
    };


    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.offset(20);
        make.centerY.offset(0);
    }];
    [self.genderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.offset(0);
        make.leading.equalTo(weakSelf.titleLabel.mas_trailing).offset(20);
        make.width.mas_equalTo(100);
    }];
    [self.femaleRichBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.bottom.offset(0);
        make.width.mas_equalTo(45);
    }];
    [self.maleRichBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.trailing.offset(0);
        make.width.equalTo(weakSelf.femaleRichBtn.mas_width);
    }];
}

- (void)setItem:(PropertyItem *)item {
    _item = item;
    self.titleLabel.text = item.inputTitle;
}

- (NSString *)getGenderString {
    if (self.femaleRichBtn.selected) {
        return @"女";
    } else {
        return @"男";
    }
}

@end
